/**
 * Copyright  (c) 2009-2013 XLab,  Org. 
 * All  rights  reserved. 
 */
package org.xlab.baseframework.test.service;

import static org.junit.Assert.*;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.xlab.baseframework.domain.ISystemRootUserDomain;
import org.xlab.baseframework.domain.SystemParameter;
import org.xlab.baseframework.exception.RootUserNotFoundException;
import org.xlab.baseframework.exception.SystemParameterDuplicateException;
import org.xlab.baseframework.exception.SystemParameterNotFoundException;
import org.xlab.baseframework.exception.SystemRuntimeException;
import org.xlab.baseframework.exception.ValidateFailedException;
import org.xlab.baseframework.service.api.ISystemParameterService;
import org.xlab.baseframework.service.spi.IUserService;
import org.xlab.baseframework.test.base.BaseTester;


/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0 Sep 10, 2010
 * @since 1.0.0
 * 
 */
@RunWith(BaseTester.class)
@ContextConfiguration("classpath:org/xlab/baseframework/conf/module/*/baseframework-*.xml")
public class SystemParameterServiceTestCase {

	private static final Logger log = Logger
			.getLogger(SystemParameterServiceTestCase.class);

	@Autowired
	@Qualifier("bpf_systemParameterService")
	private ISystemParameterService systemParameterService;

	@Autowired
	@Qualifier("bpf_defaultUserService")
	private IUserService userService;

	@Test
	public void testCreateParameter() {
		try {
			log.debug("Creating root user domain...");
			ISystemRootUserDomain root = userService.getSystemRootUser();
			if (root != null) {
				log.debug("Creating system parameter.");
				this.systemParameterService.createBasicParameter(
						"TESTING-CODE", "TESTING-VALUE", "CREATED BY TESTING",
						"NO MEANING", root);
			}
			assertEquals("TESTING-VALUE", this.systemParameterService
					.findByCode("TESTING-CODE", true).getValue());
		} catch (RootUserNotFoundException e1) {
			e1.printStackTrace();
		} catch (ValidateFailedException e1) {
			e1.printStackTrace();
		} catch (SystemParameterNotFoundException e) {
			e.printStackTrace();
		} catch (SystemRuntimeException e) {
			e.printStackTrace();
		} catch (SystemParameterDuplicateException e) {
			log.warn("Parameter has been created, now removing...");
			SystemParameter param = null;
			try {
				param = this.systemParameterService.findByCode("TESTING-CODE",
						true);
			} catch (SystemParameterNotFoundException e1) {
				e1.printStackTrace();
			} catch (SystemRuntimeException e1) {
				e1.printStackTrace();
			}
			this.systemParameterService.deleteParameter(param);
		}
	}
}
